package com.honglang.module.article.action;

import java.io.IOException;

import com.honglang.aop.impl.UpdateSearchIndexPointCut;
import com.honglang.aop.impl.UpdateSideBarHtmlPointCut;
import com.honglang.common.tools.Constants;
import com.honglang.module.article.entity.ArticleCategories;
import com.honglang.module.article.service.ArticleCategoriesService;
import com.honglang.struts2.action.BaseAction;
import com.opensymphony.xwork2.Action;

public class ArticleCategoryManagerAction extends BaseAction{
	
	private ArticleCategoriesService articleCategoriesService;

	/**
	 * action to do before entering the articleCategoryManagerPage
	 * @return
	 */
	public String preArticleCategoryManager(){
		
		this.getRequest().setAttribute("artcleCategories", articleCategoriesService.getAll());
		return Action.SUCCESS;
	}
	
	
	/**
	 * save the articleCategory if not exist ;
	 * @return
	 * @throws IOException
	 */
	@UpdateSideBarHtmlPointCut
	@UpdateSearchIndexPointCut
	public String save() throws IOException{
		String articleCategoryName = this.getRequest().getParameter("articleCategoryName");
		if(this.articleCategoriesService.isCategoryNameExist(articleCategoryName)){
			this.getResponse().getWriter().write("false");
			
		}else{
			
			ArticleCategories aritlceCategory = new ArticleCategories();
			aritlceCategory.setCategoryName(articleCategoryName);
			this.articleCategoriesService.save(aritlceCategory);
			this.getRequest().setAttribute("feedback", "It's done !");
			this.getResponse().getWriter().write("success");
		}
		return Action.NONE;
		
	}
	
	
	/**
	 * update the selected articleCategory ;
	 * @return
	 * @throws IOException
	 */
	@UpdateSideBarHtmlPointCut
	@UpdateSearchIndexPointCut
	public String update() throws IOException {
		Integer articleCategoryId = Integer.valueOf(this.getRequest().getParameter(Constants.Article_Category_ID));
		String articleCategoryName = this.getRequest().getParameter("articleCategoryName");
		ArticleCategories articleCategory = this.getArticleCategoriesService().getById(articleCategoryId);
		articleCategory.setCategoryName(articleCategoryName);
		this.articleCategoriesService.save(articleCategory);
		this.getResponse().getWriter().write("Update Done !");
		return Action.NONE;
		
	}
	
	
	@UpdateSideBarHtmlPointCut
	@UpdateSearchIndexPointCut
	public String delete() throws IOException{
		
		Integer articleCategoryId = Integer.valueOf(this.getRequest().getParameter(Constants.Article_Category_ID));
		this.articleCategoriesService.delete(this.articleCategoriesService.getById(articleCategoryId));
		this.getResponse().getWriter().write("Delete Done !");
		return Action.NONE;
	}
	
	
	public ArticleCategoriesService getArticleCategoriesService() {
		return articleCategoriesService;
	}

	public void setArticleCategoriesService(
			ArticleCategoriesService articleCategoriesService) {
		this.articleCategoriesService = articleCategoriesService;
	}
	
}
